In ASP uses the SQL sentence (total collection) |http://www.cshu.net




                               About us 
                               Commercial cooperation 
                               Copyright declaration 
                               Contacts with us 



            Returns to the home pageArticle browsingOther columnsLands the forum


            |   The absolute &#21019;   |   |   hacker file   |   |   is newest 
            dynamically   |   
                  |  In hackerfile>> classicalmaterial >>ASP uses the SQL 
                  sentence (total collection)  Printing

            In ASP uses the SQL sentence (total collection)
            Www.cshu.net  2003-4-30  fog rain village 

              Perhaps all kinds of SQL product were many awfully, you early 
              cannot attend to other to cast off the sleeve to begin to do. But 
              you must simultaneously use ASP and the SQL speech possibly can be 
              dizzy. MySQL, SQL Server and mSQL all are certainly the good SQL 
              tool, what a pity, you actually cannot use them under the ASP 
              environment to found the practical SQL sentence. But, you may use 
              the Access knowledge as well as the corresponding Access skill 
              which oneself grasps, in addition our prompt and the skill, 
              believed certainly can successfully join SQL in yours ASP 
              homepage. 

              In the SQL world, extremely the foundation operation was the 
              SELECT sentence. Directly uses SQL under the database tool time 
              very many people to be able to be familiar with the operation: 
              SELECT what 
              FROM whichTable 
              WHERE criteria 
              Above carries out the sentence to be able to found to deposit its 
              result the inquiry. 
              Above but in the ASP page document, you also may use the general 
              grammar, but the situation is slightly different, ASP programs, 
              the SELECT sentence content must bestow on as the character string 
              for a variable: 
              SQL = "SELECT what FROM whichTable WHERE criteria" 
              Good, had understood under ASP SQL "speaks" the way, receives 
              follows a set pattern then, so long as meets your needs, the 
              traditional SQL inquiry pattern and the condition inquiry all can 
              have the use. 
              Explains with examples, the might as well supposition in your 
              database has a data sheet, the name is Products, now you want to 
              take out this outside and inside the complete record. Then you 
              have compiled the under code: 
              SQL = "SELECT * FROM Products" 
              After above the code SQL sentence function will be takes out in 
              the table the complete data execution to be able to select in 
              the data sheet the complete record. But, if only wants to take out 
              some specific row from the table, for instance p_name. That could 
              not use * ݬ, here entered the specific some row name, the code 
              as follows: 
              SQL = "SELECT p_name FROM Products" 
              After above carries out inquires in the Products table, a p_name 
              row content can all select. 

              Some times take out the complete database recording perhaps happen 
              to to satisfy your request, but, we usually only must obtain the 
              partial records in the majority situation. How then should design 
              the inquiry? Certainly could beat a brains, much less this article 
              also intentionally did not want to let you use that any nuisance 
              recordset. 
              Giving an example, if you only planned takes out the p_name 
              record, moreover these recording names must by the letter w 
              drivehead, then you had to use under WHERE ϣ: 
              SQL = "SELECT p_name FROM Products WHERE p_name LIKE 'W%'" 
              WHERE key word behind with is using for to filter the data the 
              condition, had these conditions help, only has satisfies the 
              certain standard the data only then to be able to inquire. In the 
              above example, the inquiry result only can obtain the name by w 
              the drivehead p_name record. 
              Above in example, percentage mark (%) the meaning is instructed 
              the inquiry returns to all w letter drivehead moreover behind is 
              any data does not even have the data the record clause. Therefore, 
              inquires above the execution, west and willow can select from the 
              Products table and deposits in the inquiry. 
              Likes such which you sees, so long as carefully designs the SELECT 
              sentence, you may limit the information content which in recordset 
              returns, ponders over always can satisfy your request. 
              These also only were grasps the SQL use just to start. In order to 
              help you gradually to grasp the complex SELECT sentence usage, 
              under lets us look at the essential standard terminology again: 
              Compared with the operator, these gadget all are you in construct 
              own SELECT character string to obtain time the specific data must 
              pass through commonly used arrives. 
              WHERE ϣ foundation 
              Is starting founds WHERE ϣ time, the simplest way is uses the 
              standard the comparison mark, they are <, <=, >, >=, and =. Below 
              obviously, you are very quick can understand the code meaning and 
              the concrete movement result: 
              SELECT * FROM Products WHERE p_price >= 199.95 
              SELECT * FROM Products WHERE p_price 19.95 
              SELECT * FROM Products WHERE p_version = '4' 
              Attention: Here you can note, last in the illustrative sentence 
              numeral around 4 added the single quotes. The reason is such, in 
              this example '4' is the text type but the non- digital type. 
              Because you can put the SELECT sentence to the quotation mark in 
              come it to take a value bestows on for the variable, therefore you 
              also may use the quotation mark in the sentence. 
              Compared with operator 
              Assigns compared with the operator from the table to take out the 
              data the content scope. You may use them to found the filter in 
              order to reduces recordset the scope, urges it only to preserve 
              assigns the information which under the duty you cared about. 
              You already took out in the example in above which the w drivehead 
              recorded to see the LIKE usage. LIKE sentences the decisive remark 
              is an extremely useful mark. But, used it in very many situations 
              possibly to be able to take to you too many data, therefore in 
              used in front of it should better first use one's brains thought 
              oneself wanted to obtain any data. The supposition you want to 
              take out 5 digital the SKU numbers, moreover its opening is 1 
              ending is 5, then you may use to delimit the symbol (_) to replace 
              % the mark: 
              SQL = "SELECT * FROM Products WHERE p_sku LIKE '1___5'" 
              Next delimits a symbol expression wilfully character. Therefore in 
              inputs "1 _ _ _ 5" in the situation, your search could limit in 
              satisfies the specific pattern in 5 figures scope. 
              If you want to do exactly the opposite, must discover all does not 
              match "1_ _ _ 5" the pattern SKU clause. Then you only needed in 
              in front of sentence example LIKE to add on NOT to be allowed a 
              moment ago. 
              BETWEEN 
              The supposition you want to take out in the certain scope the 
              data, moreover you beforehand know the scope the beginning and the 
              end point, then you might as well use BETWEEN copula. Now lets our 
              supposition you want to select assigns in the table the scope in 1 
              and 10 between record. You may as follows use BETWEEN: 
              ... WHERE ID BETWEEN 1 AND 10 
              Or you also may use mathematics judgement words and expressions 
              which already was familiar with: 
              ... WHERE ID >= 1 AND ID >= 10 
              We so far talk about the SQL sentence relative is simple, if again 
              can through the standard recordset circulation inquiry, then these 
              sentences also can satisfy some more complex request. But, why 
              must rigidly adhere in above the foundation standard which 
              superficial stops? You definitely may again increase other some 
              marks, for instance AND, OR and NOT complete a more formidable 
              function. 
              Take the under SQL sentence as the example: 
              SQL = "SELECT c_firstname, c_lastname, c_email FROM customers 
              WHERE c_email IS 
              NOT NULL AND c_purchase = '1' OR c_purchase = '2' AND c_lastname 
              LIKE 
              'A%' " 
              The SQL knowledge at present grasps which on you, the above 
              example is not difficult to explain, how but does the above 
              sentence certainly not very clearly let you see clearly the 
              condition words and expressions is the agglutination in the sole 
              SQL sentence. 
              Good sentence 
              Not good understands in the SQL sentence in the situation, you 
              might as well decompose the entire sentence into the good code, 
              then gradually increases the inquiry sentence in the existing 
              variable foundation each constituent and exists it in the 
              identical variable: 
              SQL = "SELECT c_firstname, c_lastname, c_emailaddress, c_phone" 
              SQL = SQL & "FROM customers" 
              SQL = SQL & "WHERE c_firstname LIKE 'A%' and c_emailaddress NOT 
              NULL" 
              SQL = SQL & "ORDER BY c_lastname, c_firstname" 
              To last, the SQL variable has contained the below integrity SELECT 
              sentence: 
              "SELECT c_firstname, c_lastname, c_emailaddress, c_phone FROM 
              customers 
              WHERE c_firstname LIKE 'A%' and c_emailaddress NO NULL ORDER BY 
              c_lastname, 
              C_firstname " 
              The entire sentence decomposes after above obviously good read 
              many! Is carrying on debugs, you perhaps are glad knock several 
              characters the procedure to change well read. But you may have to 
              remember, in front of seal quotation mark or after opens the 
              quotation mark you needs to increase the blank space, like this 
              can guarantee the character string connects time you have not 
              gathered several words. 

              After has learned the SELECT sentence structure and the use how 
              should you study use it. Grasps in you under the database tool, 
              this possibly meant you must press down some to write "the 
              execution" the inscription button. On the ASP homepage, may carry 
              out the SQL sentence also to be allowed immediately to treat as 
              the memory procedure call. 
              Once founded the SQL sentence, you also must try to visit its 
              inquiry result. Obviously, here key is ASP recordset. When uses 
              non- SQL recordset, founds recordset the code to be usual as 
              follows shows: 
              Dim objRec 
              Set objRec = Server.CreateObject ("ADODB.Recordset") 
              ObjRec.Open "customers", objConn, 0, 1, 2 
              If you may not be strange to the ASP quite familiar above code to 
              you, you should know "customers" expressed you open in the 
              database a data sheet name. 
              Opens recordset 
              In order to fully uses you more familiar SQL skill, you need to 
              adjust recordset which on the conventional ASP homepage most often 
              uses: 
              Dim objRec 
              Set objRec = Server.CreateObject ("ADODB.Recordset") 
              ObjRec.Open SQL, objConn, 0, 1, 2 
              Here only revision is in objRec.Open, with contained the SQL 
              sentence the variable to replace the data sheet name afterwards 
              which had to inquire. 
              One of this method merits is you may assign the vernier type (for 
              example above 0, 1,2 to show). 
              Carries out SQL 
              You may use compact line of codes to carry out the SQL sentence to 
              found recordset. Below is the grammar: 
              Dim objRec 
              Set objRec = objConn.Execute (SQL) 
              In on example, you saw SQL is you deposits oneself SQL the SELECT 
              sentence the variable. This line of code "the movement" the SQL 
              sentence (or said carries on inquiry to database), the access and 
              stores the data in recordset, in on example is variable objRec. 
              This method main shortcoming is the vernier type which you cannot 
              choose oneself wants to use. On the contrary, recordset always 
              opens with front to the vernier. 
              Perhaps because of the vernier reason, you planned the familiar 
              two kinds found recordset the method. The direct execution inquiry 
              saved has entered the time which the character consumed, but such 
              speech you had to use the vernier which tacitly approved, like 
              this had the possibility bitter experience frequently not to be 
              able the normal operation problem. Which means no matter you 
              specifically do use, two between biggest difference nothing more 
              than code is also concise or not. In did not consider you obtain 
              any field, your standard are any under the premise, also no matter 
              your how stored datum, to use SQL type recordset to be able 
              standard recordset which opens ASP on to have to be much smaller 
              than in the volume, was indescribable to the simplicity which 
              operated. After all, through filters the data, you eliminated have 
              consumed the circulation which the time the if-then test and 
              possibly used. 
              The compilation measured tests SQL 
              Here has a skill, many specialized ASP programmer is familiar with 
              in test homepage time "compilation" own SQL sentence. Does this 
              may help you to debug the code, because you may see transmits 
              character string which carries out to the server. But you must do 
              nothing but are increase Response.WriteyourVariable to demonstrate 
              the related information on the screen. In you submits with the SQL 
              related question for ASP discussion group's time you should the 
              enclosed these information. 

              In ASP uses 6:th SQL sentence Memory inquiry 

              When your inquiry is relatively simple, each time from the 
              beginning starts to found the SQL sentence not to be any time 
              comsuming, but, the complex inquiry has been different, each time 
              all from the beginning comes to be able to make the very many 
              developments mistake. Therefore, once lets SQL smoothly move, you 
              should better save them, when need again transfers them. Thus, 
              even if was the inquiry sentence which simply inquired you all to 
              be able to use to save as necessary. 
              The supposition you each week all must make a time of report to 
              the team, pointed out at present exists the service support 
              question, these data need to select from yours database, moreover 
              must defer to the date to choose the record, simultaneously acts 
              according to you the support question category arrangement which 
              uses in the team. Once you have designed this inquiry, you why 
              later each week all rewill compile time? Do not found the inquiry 
              on yours HTML page, you should use your database tool to found 
              inquire and preserve it. Then you may use the ActiveCommand 
              attribute to insert the inquiry to yours ASP homepage. First 12 
              chapter of you possibly can think what doesn't have meaning, 
              actually also on several lines of codes: 
              Set objSQ = Server.CreateObject ("ADODB.Command") 
              ObjSQ.ActiveConnection = "databaseName" 
              ObjSQ.CommandText = "storedQueryName" 
              ObjSQ.CommandType = adCmdStoredProc 
              Set objRec = objSQ.Execute 
              The attention, uses adCmdStoredProc to express you have already 
              contained the adovbs.inc document on the page. This file 
              definition you have been allowed to defer to the name but the non- 
              numeral to carry on the visit the Access constant. Only needs on 
              the page to contain this document then? ! --#include -->), then 
              you were allowed to use adCmdStoredProc this kind of name. Thus, 
              future you again saw time is easier above to understand the 
              inquiry which saves is any meaning. 

              In ASP uses 8:th SQL sentence Random number 
              Establishes the stochastic production the HTML code is the ASP 
              characteristic which is quite easy to realize. You possibly found 
              "a placard, have rolled every day" advertisement and so on, will 
              only need slightly to embellish can make your website to make up 
              in the course of time newly. 
              To saves the data said in the database above that, the random 
              number special performance produces the effect, but their possibly 
              too slow. You cannot request ASP "to look for a random number" 
              then to print. In fact the common solution is the circulation 
              which the establishment as follows shows: 
              Randomize 
              RNumber = Int (Rnd*499) +1 
              While Not objRec.EOF 
              If objRec ("ID") = RNumber THEN 
              ... Here is carries out the script... 
              End if 
              ObjRec.MoveNext 
              Wend 
              This is very easy to understand. First, you take out 1 to a 500 
              scopes in random number (supposition 500 are total which in 
              database records). Then, you spread the calendar each record to 
              test ID the value, inspect it whether matches RNumber. Satisfies 
              the condition the speech on the execution by the THEN essential 
              start of word that together code. If your RNumber is equal to 495, 
              then had to circulate database flower's time to be possible to be 
              long. Although 500 this numerals looked like big, but compared 
              stabler enterprise solution this or a small database, latter has 
              usually contained the tens of thousands of strip record in a 
              database. This time not died decides? 
              Uses SQL, you may very quickly discover the accurate recording and 
              to open only to contain this record recordset, as follows shows: 
              Randomize 
              RNumber = Int (Rnd*499) + 1 
              SQL = "SELECT * FROM Customers WHERE ID =" & RNumber 
              Set objRec = ObjConn.Execute (SQL) 
              Response.WriteRNumber & "=" & objRec ("ID") & "" & objRec 
              ("c_email") 
              Does not need to write RNumber and ID, you only need to inspect 
              the match situation then. So long as you to above code work 
              satisfaction, you from may according to have to operate "are 
              stochastic" the record. Recordset has not contained other 
              contents, therefore you were very quick the recording which could 
              find you to need like this greatly to reduce the process time. 
              Again discusses the random number 
              Now you set firm resolve to have to press out do the Random 
              function last the drop of oil, then your possibly meeting time 
              takes out the multi- strip stochastic recording or wants to use in 
              the certainly stochastic scope the record. Expanded the above 
              standard Random demonstration may use SQL to be supposed to above 
              two kind of situations. 
              In order to take out several stochastic choices the recording and 
              deposits in identical recordset, you may save three random 
              numbers, then the inquiry database obtains matches these digital 
              the recording: 
              SQL = "SELECT * FROM Customers WHERE ID =" & RNumber & "OR ID =" & 
              RNumber2 & "OR ID =" & RNumber3 
              (Perhaps when you want to select 10 records are each time page 
              loads 10 links tabulate), you may use BETWEEN or mathematics 
              equality select first record and the suitable quantity increase 
              progressively the record. This operation may complete through the 
              quite several ways, but the SELECT sentence only demonstrated one 
              kind of possibility (here ID is automatic production number): 
              SQL = "SELECT * FROM Customers WHERE ID BETWEEN" & RNumber & "AND" 
              & RNumber & "+ 9" 
              Attention: Above the code carries out the goal is not inspects in 
              the database whether has 9 concurrent records. If you need to 
              guarantee each time selects 10 records, then you must further 
              design the inquiry. 

              In ASP uses 9:th SQL sentence Table list operation 

              Takes out the information from some page table list is in the ASP 
              programming the common question. But, spreads the calendar to be 
              able to spend through the table list transmission record to go to 
              the long time? This is decided by the database size. The simple 
              GUI contact surface all possibly command circulates the calendar 
              operation consumption too much time. 
              For instance, the supposition has the team members to register the 
              GUI screen to input between own name surname and the name with 
              comma connects: Amy.cowen. This value only submits through the 
              table, her current project tabulated on takes from the database 
              came out and the demonstration on the screen. In order to fast 
              takes out the user the recording in order to below demonstrated on 
              the screen, you may compile the code. 
              On below the supposition HTML page contains the code: 
              <FORM ACTION= "login_post.asp" METHOD= "POST" > 
              <INPUT TYPE= "text" NAME= "dotname" > 
              <INPUT TYPE= "submit" > 
              </FORM> 
              In yours login_post.asp code page you hoped reads takes the 
              dotname field the value, simultaneously discovers the recording 
              which in the database matches: 
              SQL = "SELECT dotname, ID FROM team WHERE dotname = '" & 
              Request.Form ("dotname") & "'" 
              If you use the GET method, needs to do the database recording with 
              the QueryString in field value the comparison, then you may use 
              the similar essential method: 
              SQL = "SELECT dotname, ID FROM team WHERE dotname = '" & 
              Request.QueryString ("dotname") & "'" 
              Certainly, you also may assign the table single field value for 
              some variable and insert it to in yours SQL sentence: 
              Dotname = Request.QueryString ("dotname") 
              SQL = "SELECT dotname, ID FROM team WHERE dotname = '" & dotname & 
              "'" 

              In ASP uses 10:th SQL sentence View 
              If on the page has very many records, in very many situations, can 
              rise the foreword arrangement demonstration efficiency to these 
              information according to the alphabet ˽ arrangement the tradition 
              to be higher than. Uses ORDER which you is familiar with BY ϣ, 
              you may very easily increase this function on the page, lets the 
              user control its recording demonstration the view. 
              The supposition you have a page the name to call customers.asp, 
              the page to hold the guild to select from the Customers table 
              completely record and defer to the alphabet order to arrange to 
              the customer surname. Formerly we already looked had been to the 
              arrangement code, in order to realize the reverse arrangement, you 
              only needed after ORDER the BY expression added on desc then: 
              SQL = "SELECT c_lastname, c_firstname, c_email FROM Customers 
              ORDER BY c_lastnamedesc" 
              Must under the static pattern this also pretty good. Perhaps above 
              but, you want to revise the code to cause it to provide the new 
              GUI function: 
              <A HREF= "customers.asp" >Sort Ascending</A> | <A HREF= 
              "customers.asp? Sort=desc ">Sort Descending</A> 
              <P> 
              <% 
              SQL = "SELECT c_lastname, c_firstname, c_email FROM Customers 
              ORDER BY c_lastname" & sort & "" 
              Set objRec = objConn.Execute (SQL) 
              While Not objRec.EOF 
              Response.WriteobjRec ("c_lastname") & "," & objRec ("c_firstname") 
              & " 
              "& objRec ("c_email") &" <P> " 
              ObjRec.MoveNext 
              Wend 
              %> 
              Above the code most begins the link permission user revises the 
              page the view. You may the heavy load identical page examine these 
              information, but, in Sort in the Descending link situation, the 
              page personally meets for the sort variable evaluation. Then, on 
              has not assigned desc in the SQL sentence but is uses the sort 
              variable the value. 
              Under must do is reads takes page crown sort the value. 
              <% 
              Sort = Request.Querystring ("sort") 
              %> 
              Above the code picks out sort from URL to be possible to use the 
              value. 
              Certainly has not made you to spend too many times, you also may 
              adjust the identical page permission user to the specific row 
              arrangement. Simple SELECT menu table accuse in a written document 
              lists SORT the BY option is possibly a more convenient measure. 
              In the definite database has how many records, or the 
              determination had how many recording to achieve certain standards, 
              these completed by no means the difficult matter with ASP. If you 
              have used the correct vernier type, you may use the RecordCount 
              attribute to obtain the recording number certainly also to be 
              allowed to use recordset. But, has simpler means, this is uses 
              count in own SELECT sentence (*), the code as follows shows: 
              SQL = "SELECT count (*) FROM Customers" 
              Or 
              SQL = "SELECT count (*) FROM Customers WHERE c_lastname LIKE 'A%'" 

              Explains with examples, following code will select some recording 
              as well as these recording total: 
              SQL = "SELECT c_firstname, c_lastname, count (*) FROM Customers 
              WHERE c_lastname LIKE 'A%'" 
              But you cannot realize own goal. Here uses "count" the function 
              actually is one kind of set function, the meaning is only returns 
              to the specific information: Replied you proposed question. Said 
              to the 1st SELECT sentence that, the question is "how many records 
              has in the customer table?" The inquiry returns to the sole value 
              to take the response, therefore it cannot with your convention 
              inquiry facies group. If you hoped obtains other data, you need to 
              use RecordCount. 
              The set function besides "count" also includes AVG, MIN, MAX and 
              SUM and so on.



              Original author: ` 
              Origin: ` 
              Altogether has 38 readers to read this article 

              [Tells friend] 
            Previous article:Opens 3,389 codes (CPP) 

            Next article:Microsoft has issued the Windows server 2,003 guides 
            and the template 

            - this week popular article - related article 
            In ASP uses the SQL sentence (total collection)
            Opens 3,389 codes (CPP)
            Port greatly entire Chinese edition
            How raises own programming level
            Enters the server using the Microsoft newest strict heavy duty 
            WebDAV long-distance buffer overflow loophole! (In view of English 
            version method) 
            A WEBDAVX overflow success invasion test
            The WebDav long-distance overflow loophole analyzes



      CSHU 
